package com.mdlib.helper.module.account.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.ljlib.core.widget.CountdownButton;
import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.common.MDConstant;
import com.mdlib.helper.event.LoginEvent;
import com.mdlib.helper.interfaces.ITextChangedListener;
import com.mdlib.helper.model.AccountModel;
import com.mdlib.helper.model.BaseResult;
import com.mdlib.helper.presenter.LoginHelper;
import com.mdlib.helper.ui.UIHelper;
import com.mdlib.helper.utils.MDAppUtils;
import com.mdlib.helper.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by librabin on 16/11/24.
 */

public class SmsLoginFragment extends BaseTitleFragment {

    public static SmsLoginFragment newInstance() {
        return new SmsLoginFragment();
    }



    @Bind(R.id.btn_featch_code)
    CountdownButton mFeatchCodeButton;

    @Bind(R.id.et_sms_phone)
    EditText mPhoneNumField;

    @Bind(R.id.et_sms_code)
    EditText mPhoneNumCode;



    @Bind(R.id.tv_sms_submit)
    TextView mTvSmsSubmit;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_sms_login;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setCommonTitle(R.string.sms_login_title);

        mPhoneNumField.addTextChangedListener(new ITextChangedListener(){
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 11 && !mFeatchCodeButton.isRunning()) {
                    mFeatchCodeButton.setEnabled(true);
                } else {
                    mFeatchCodeButton.setEnabled(false);
                }
            }
        });
    }


    @OnClick({R.id.btn_featch_code, R.id.tv_sms_submit})
    public void onClick(View view) {
        String phoneNum = mPhoneNumField.getText().toString();
        if (!MDAppUtils.checkPhoneNum(phoneNum)) {
            return;
        }
        switch (view.getId()) {
            case R.id.btn_featch_code:
                featchCode(phoneNum, MDConstant.PHONE_CODE_TYPE_LOGIN);
                break;
            case R.id.tv_sms_submit:
                String phoneCode = mPhoneNumCode.getText().toString();
                if (TextUtils.isEmpty(phoneCode)) {
                    ToastUtil.showToast("手机验证码不能为空");
                }
                LoginHelper.doMsgLogin(phoneNum, phoneCode);
                break;
        }
    }


    private void featchCode(String phoneNum, String codeType) {
        addSubscribe(DataManager.getInstance().getAccountApi()
                .featchPhoneCode(phoneNum, codeType)
                .compose(RxHelper.<BaseResult>rxSchedulerHelper())
                .subscribe(new RxSubscribe<BaseResult>() {
                    @Override
                    protected void onSucc(BaseResult baseResult) {
                        String a="";
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.showToast(msg);
                    }
                }));

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(LoginEvent event) {
        stopLoadingDialog();
        if (event.code == MDConstant.STATE_SUCC) {
            ToastUtil.showToast("登录成功");
            AccountModel.getInstance().setLogin(true);
            UIHelper.showMainPage(getActivity());
        } else {
            ToastUtil.showToast(event.msg);
        }
    }
}
